home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / gfx / misc / gnuplot-3.7src.lha / gnuplot-3.7src / gnuplot-3.7.lha / gnuplot-3.7 / os9.c < prev    next >
C/C++ Source or Header  |  1998-04-15  |  5KB  |  214 lines

  1. /* $Id: os9.c,v 1.2 1998/03/22 22:31:54 drd Exp $ */
  2.  
  3. /* GNUPLOT - os9.c */
  4.  
  5. /*[
  6.  * Copyright 1986 - 1993, 1998   Thomas Williams, Colin Kelley
  7.  *
  8.  * Permission to use, copy, and distribute this software and its
  9.  * documentation for any purpose with or without fee is hereby granted,
  10.  * provided that the above copyright notice appear in all copies and
  11.  * that both that copyright notice and this permission notice appear
  12.  * in supporting documentation.
  13.  *
  14.  * Permission to modify the software is granted, but not the right to
  15.  * distribute the complete modified source code.  Modifications are to
  16.  * be distributed as patches to the released version.  Permission to
  17.  * distribute binaries produced by compiling modified sources is granted,
  18.  * provided you
  19.  *   1. distribute the corresponding source modifications from the
  20.  *    released version in the form of a patch file along with the binaries,
  21.  *   2. add special version identification to distinguish your version
  22.  *    in addition to the base release version number,
  23.  *   3. provide your name and address as the primary contact for the
  24.  *    support of your modified version, and
  25.  *   4. retain our contact information in regard to use of the base
  26.  *    software.
  27.  * Permission to distribute the released version of the source code along
  28.  * with corresponding source modifications in the form of a patch file is
  29.  * granted with same provisions 2 through 4 for binary distributions.
  30.  *
  31.  * This software is provided "as is" without express or implied warranty
  32.  * to the extent permitted by applicable law.
  33. ]*/
  34.  
  35.  
  36. /*
  37.  * Some Unix like functions that gnuplot uses.
  38.  * Original sources from the blars lib.
  39.  */
  40.  
  41. #include <stdio.h>
  42. #include <modes.h>
  43. #include <direct.h>
  44. #include <sgstat.h>
  45.  
  46. #ifdef PIPES
  47.  
  48. /* Original version by Robert A. Larson */
  49. /* Adapted by M.N. Schipper */
  50.  
  51. #include <string.h>
  52. #include <module.h>
  53.  
  54. extern char *_environ;
  55. extern int os9fork();
  56. extern mh_com *modlink();
  57. extern mh_com *modloadp();
  58.  
  59. static int proc[_NFILE];
  60. static mh_com *loadmods[_NFILE];
  61.  
  62. FILE *popen(command, mode)
  63. char *command;
  64. char *mode;
  65. {
  66.     int temp, fd;
  67.     FILE *pipe;
  68.     char *argv[4];
  69.     register char *cp;
  70.     mh_com *mod;
  71.     int linked = 0;
  72.  
  73.     if(mode[1]!='\0' || (*mode!='r' && *mode!='w')) return (FILE *)NULL;
  74.     fd = (*mode=='r');
  75.     if((temp = dup(fd)) <= 0) return (FILE *)NULL;
  76.     if((pipe = fopen("/pipe", "r+")) == NULL) {
  77.         close(temp);
  78.         return (FILE *)NULL;
  79.     }
  80.     close(fd);
  81.     dup(fileno(pipe));
  82.  
  83.     if (strrchr (command, '/') == NULL)
  84.         mod = modlink (command, 0);
  85.     else
  86.         mod = (mh_com *) -1;
  87.     if (mod == (mh_com *) -1)
  88.         loadmods[fileno(pipe)] = mod = modloadp (command, 0, NULL);
  89.     else
  90.     {
  91.         linked = 1;
  92.         loadmods[fileno(pipe)] = (mh_com *) -1;
  93.     }
  94.         
  95.     argv[0] = "shell";
  96.     if (mod != (mh_com *) -1)
  97.     {
  98.         argv[1] = "ex";
  99.         argv[2] = command;
  100.         argv[3] = (char *)NULL;
  101.     }
  102.     else
  103.     {
  104.         argv[1] = command;
  105.         argv[2] = (char *)NULL;
  106.     }
  107.     if((proc[fileno(pipe)] = os9exec(os9fork, argv[0], argv, _environ, 0, 0)) < 0)
  108.     {
  109.         fclose(pipe);
  110.         pipe = NULL;
  111.     }
  112.     close(fd);
  113.     dup(temp);
  114.     close(temp);
  115.     if (linked && mod != (mh_com *) -1)
  116.         munlink (mod);
  117.     return pipe;
  118. }
  119.  
  120. int pclose(pipe)
  121. FILE *pipe;
  122. {
  123.     int p, stat, w;
  124.  
  125.     if((p = proc[fileno(pipe)]) <= 0) return -1;
  126.     proc[fileno(pipe)] = 0;
  127.     fflush(pipe);
  128.     if (loadmods[fileno(pipe)] != (mh_com *) -1)
  129.         munlink (loadmods[fileno(pipe)]);
  130.     fclose(pipe);
  131.     while((w=wait(&stat)) != -1 && w!=p) {}
  132.     return w==-1 ? -1 : stat;
  133. }
  134.  
  135. #endif    /* PIPES */
  136.  
  137.  
  138. int isatty(f)
  139. int f;
  140. {
  141.     struct sgbuf sgbuf;
  142.  
  143.     if(_gs_opt(f, &sgbuf) < 0) return -1;
  144.     return sgbuf.sg_class == 0;
  145. }
  146.  
  147.  
  148. char *getwd(p)
  149. char *p;
  150. {
  151.     register char *cp;
  152.     register struct dirent *dp;
  153.     register int l, olddot = 0, i, d, dot, dotdot;
  154.     struct dirent db[8];
  155.     char buf[1024];
  156.  
  157.     cp = &buf[1024-1];
  158.     *cp = '\0';
  159.     for(;;) {
  160.     if((d = open(".", S_IREAD | S_IFDIR)) < 0) {
  161.         if(*cp) chdir(cp+1);
  162.         return NULL;
  163.     }
  164.     if((i = read(d, (char *)db, sizeof(db))) == 0) {
  165.         if(*cp) chdir(cp+1);
  166.         close(d);
  167.         return NULL;
  168.     }
  169.     dotdot = db[0].dir_addr;
  170.     dot = db[1].dir_addr;
  171.     if(olddot) {
  172.         i -= 2 * sizeof(struct dirent);
  173.         dp = &db[2];
  174.         for(;;) {
  175.             if(i <= 0) {
  176.             if((i = read(d, (char *)db, sizeof(db))) == 0) {
  177.             if(*cp) chdir(cp+1);
  178.             close(d);
  179.             return NULL;
  180.             }
  181.             dp = &db[0];
  182.         }
  183.         if(olddot == dp->dir_addr) {
  184.             l = strlen(dp->dir_name);
  185.             /* last character has parity bit set... */
  186.             *--cp = dp->dir_name[--l] & 0x7f;
  187.             while(l) *--cp = dp->dir_name[--l];
  188.             *--cp = '/';
  189.             break;
  190.         }
  191.         i -= sizeof(struct dirent);
  192.         dp++;
  193.         }
  194.     }
  195.     if(dot==dotdot) {
  196.         if(*cp) chdir(cp+1);
  197.         *p = '/';
  198.         if(_gs_devn(d, p+1) < 0) {
  199.             close(d);
  200.         return NULL;
  201.         }
  202.         close(d);
  203.         strcat(p, cp);
  204.         return p;
  205.     }
  206.     close(d);
  207.     if(chdir("..") != 0) {
  208.         if(*cp) chdir(cp+1);
  209.         return NULL;
  210.     }
  211.     olddot = dot;
  212.     }
  213. }
  214.